package com.zhoug.demo.player.play;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.TextureView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.zhoug.common.utils.StringUtils;
import com.zhoug.common.utils.ToastUtils;
import com.zhoug.demo.player.Monitor;
import com.zhoug.demo.player.MonitorDialog;
import com.zhoug.demo.player.MyBaseActivity;
import com.zhoug.demo.player.http.ApiRepository;
import com.zhoug.demo.player.http.DataCallback;
import com.zhoug.demo.templete.R;
import com.zhoug.logging.Logger;
import com.zhoug.player.exoplayer.v2.ZExoPlayer;
import com.zhoug.zmui.list.OnSelectChangeListener;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.media3.common.util.UnstableApi;


@UnstableApi
public class ExoActivity extends MyBaseActivity {
    private static final String TAG = ">>>MainActivity";
    private ZExoPlayer mPlayer;

    private MonitorDialog mMonitorDialog;
    private ApiRepository mApiRepository;
    private Monitor mMonitor;
    private TextureView mTextureView;
    private Button mBtnStartPlay;
    private EditText mEtUrl;
    private TextView mBtnClearUrl;
    private Button mBtnGetPlayUrl;
    private Button mBtnPausePlay;
    private Button mBtnResumePlay;
    private Button mBtnStopPlay;
    private Button mBtnMonitor;
    private TextView mTvMonitor;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_exo;
    }

    @Override
    protected void findViews() {
        mTextureView = findViewById(R.id.textureView);
        mBtnStartPlay = findViewById(R.id.btn_startPlay);
        mEtUrl = findViewById(R.id.et_url);
        mBtnClearUrl = findViewById(R.id.btn_clearUrl);
        mBtnGetPlayUrl = findViewById(R.id.btn_getPlayUrl);
        mBtnPausePlay = findViewById(R.id.btn_pausePlay);
        mBtnResumePlay = findViewById(R.id.btn_resumePlay);
        mBtnStopPlay = findViewById(R.id.btn_stopPlay);
        mBtnMonitor = findViewById(R.id.btn_monitor);
        mTvMonitor = findViewById(R.id.tv_monitor);
    }

    @Override
    protected void onCreate1(Bundle savedInstanceState) {
        mApiRepository = new ApiRepository();
        mApiRepository.setLifecycleOwner(this);
        List<Monitor> monitors = Monitor.getMonitors();
        mMonitor = monitors.get(0);
        mTvMonitor.setText(mMonitor.getName());
        mMonitorDialog = new MonitorDialog(this);
        mMonitorDialog.setData(monitors);
        mMonitorDialog.setSelect(mMonitor);
        mMonitorDialog.setMultiSelect(false);
        mMonitorDialog.setOnSelectChangeListener(new OnSelectChangeListener<Monitor>() {
            @Override
            public void onSelectChange(@NonNull List<Monitor> select) {
                if (!select.isEmpty()) {
                    mMonitor = select.get(0);
                } else {
                    mMonitor = null;
                }
                mTvMonitor.setText(mMonitor != null ? mMonitor.getName() : "");
                if (mMonitor != null && !StringUtils.isNullOrEmpty(mMonitor.getUrl())) {
                    mEtUrl.setText(mMonitor.getUrl());
                }
            }

            @Override
            public void onClearSelect() {
                mMonitor = null;
                mTvMonitor.setText("");
            }
        });
        mBtnMonitor.setOnClickListener(v -> {
            mMonitorDialog.show();
        });

        mBtnGetPlayUrl.setOnClickListener(v -> {
            if (mMonitor == null) {
                ToastUtils.toastShort("请选择摄像头");
                return;
            }
            mApiRepository.getFirmMonitorPlayUrlById(mMonitor.getId(), 0, new DataCallback<String>() {
                @Override
                public void onSuccess(String s) {
                    if (StringUtils.isNullOrEmpty(s)) {
                        Logger.e(TAG, "播放地址为空");
                        ToastUtils.toastLong("播放地址为空");
                    } else {
                        Logger.d(TAG, "获取播放地址成功:" + s);
                        ToastUtils.toastShort("获取播放地址成功");
                        mEtUrl.setText(s);
                    }
                }

                @Override
                public void onFailure(String error) {
                    ToastUtils.toastLong(error);
                }
            });
        });
        mBtnClearUrl.setOnClickListener(v -> {
            mEtUrl.setText("");
        });
        mBtnStartPlay.setOnClickListener(v -> {
            String url = null;
            Editable text = mEtUrl.getText();
            if (text != null) {
                url = text.toString().trim();
            }
            if (TextUtils.isEmpty(url)) {
                Toast.makeText(this, "播放地址为空", Toast.LENGTH_LONG);
                return;
            }
            mPlayer.setPlayUrl(url);
            mPlayer.startPlay();
        });

        mBtnPausePlay.setOnClickListener(v -> {
            if (mPlayer != null) {
                mPlayer.pausePlay();
            }
        });

        mBtnResumePlay.setOnClickListener(v -> {
            if (mPlayer != null) {
                mPlayer.resumePlay();
            }
        });

        mBtnStopPlay.setOnClickListener(v -> {
            if (mPlayer != null) {
                mPlayer.stopPlay();
            }
        });


        initPlayer();

    }

    private void initPlayer() {
        mPlayer = new ZExoPlayer(this);
//        mPlayer.setDetectAccessUnits(true);
//        mPlayer.setIgnoreAacStream(true);
        mPlayer.setVideoTextureView(mTextureView);
        mTextureView.setKeepScreenOn(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPlayer != null) {
            mPlayer.release();
        }
    }
}